//
//  BKGradientView.swift
//  dysaidao
//
//  Created by Penlon Kim on 2023/5/16.
//  Copyright © 2023 kimpl. All rights reserved.
//

import UIKit

class BKGradientView: UIView {
    
    let gradientLayer = CAGradientLayer()
    
    override init(frame:CGRect) {
        
        super.init(frame: frame)
        
        layer.addSublayer(gradientLayer)
        
    }
    
    required init?(coder:NSCoder) {
        
        super.init(coder: coder)
        
        layer.addSublayer(gradientLayer)
        
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        gradientLayer.frame=self.bounds
        
    }
    
}

// MARK: - Public
extension BKGradientView {
    
    /**
     * point的位置  左上角0,0  右上角1,0  左下角0,1  右下角1,1
     *  0,0     0.5,0     1,0
     *   ******************
     *   *                *
     *   *                *
     *   *                *
     *   *                *
     *   *                *
     *   *                *
     *   ******************
     *  0,1     0.5,1     1,1
     */
    func bk_gradient(sp startPoint: CGPoint, ep endPoint: CGPoint, colors: [UIColor], locations: [NSNumber] = [0.0, 1.0]) {
        gradientLayer.bk_colors(colors, with: self)
        gradientLayer.locations = locations
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
    }
    
    func reloadGradientColors(_ colors: [UIColor]) {
        gradientLayer.bk_colors(colors, with: self)
    }
    
}
